/**
 * Created by lanyanhua on 2018/10/31 0031.
 */
NProgress.start();
window.onload = function () {
    NProgress.done();
};
layui.use(['element', 'table', 'form', 'jquery', 'laydate','list'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var list = layui.list;
    //数据表格
    var showdatatable = table.render({
        elem: '#tableId',
        id:'showdata',
        url: '/email/findJobEmailPage',
        limit: 10,
        page: true,
        while: param(),
        cols: [ [
            {type: 'checkbox'},
            {field: 'emailServerId', title: 'ID', align: 'center', sort: true ,width: 60},
            {field: 'emailName', title: '邮箱名称', align: 'center',width: 190},
            {field: 'jobGroupNo', title: '项目编号', align: 'center',width: 110},
            {field: 'jobGroup', title: '项目名', align: 'center',width: 150 ,templet:'<div>{{d.jobGroup.groupName}}</div>'},
            {field: 'host', title: '服务器地址', align: 'center',width: 165},
            {field: 'userName', title: '用户名', align: 'center'},
            {field: 'password', title: '密码', align: 'center'},
            {field: 'port', title: '端口', align: 'center'},
            {field: 'sendAddress', title: '发送方邮件地址', align: 'center'},
            {field: 'receivingAddress', title: '接收方邮件地址', align: 'center'},
            {field: 'ccAddress', title: '抄送方邮件地址', align: 'center'},
            {field: 'introduce', title: '介绍', align: 'center'},
            {title: '操作', templet: '#operationTpl',width: 70, align: 'center',fixed:'right'}
        ] ],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });
    //list.open(options); 表单配置
    var listOpen  = {
        elem:"#groupAdd",
        fields:{
            emailServerId:{label:'ID',type:'hidden'},
            emailName:{label: '邮箱名称'},
            jobGroupNo : {label:"项目名称",type:'select',url:'/jobGroup/existsById', valueName:'groupNo',
                        source:'/jobGroup/findJobGroupAll', displayName:'groupName'},
            host : {label:"服务器地址"},
            userName : {label:"用户名"},
            password : {label:"密码"},
            port : {label:"端口"},
            sendAddress : {label:"发送方邮件地址"},
            receivingAddress : {label:"接收方邮件地址",placeholder:"多个地址请用逗号隔开"},
            ccAddress : {label:"抄送方邮件地址",placeholder:"多个地址请用逗号隔开",verify:' '},
            introduce:{label:"项目介绍",type:"textarea" }
        },
        submit : function(res){ //表单提交事件
            $.ajax({
                type : 'post',
                url : '/email/saveEmail',
                data : res.field ,
                dataType : 'json',
                success : function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index); //再执行关闭
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                }
            });
            return false;
        },
        btns:["submit",'reset'],
        area:['70%','95%']
    };
    // 提交时间
    form.on('submit(search)', function () {
        showdatatable.reload({where: param()});
        return false;
    });
    // 编辑按钮 ， 删除按钮
    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            //console.log("编辑");
            listOpen.over = data;
            list.open(listOpen);
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.post('/email/deleteEmail',{emailId:data.emailServerId},function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        }
    });
    // 添加时间
    $("#addGroup").click(function(){
        listOpen.over = null;
        //弹出框 
        list.open(listOpen);
    });
    function  param() {
        return {
            emailName: $('[name=emailName]').val(),
            jobGroupNo: $('[name=jobGroupNo]').val()
        }
    }
});
